package com.hbusa.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.hbusa.dao.ImageDao;
import com.hbusa.dto.TbPicture;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author XiaoXin
 * @date 2021/12/27 - 0:15
 */


public class ImageDaoImpl implements ImageDao {
    public ImageDaoImpl() {
    }

    @Override
    public boolean add(TbPicture img) {
        int row = 0;
        try {
            row = Db.use().insert(
                    Entity.create("tb_picture")
                            .set("picture_url", img.getPictureUrl())
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return row > 0;
    }

    @Override
    public boolean removeById(int id) {
        int del = 0;
        try {
            del = Db.use().del(
                    Entity.create("tb_picture")
                            .set("picture_id", id)
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return del > 0;
    }

    @Override
    public TbPicture queryById(int id) {
        List<TbPicture> tbPictures = null;
        try {
            tbPictures = Db.use().find(
                    Entity.create("tb_picture")
                            .set("picture_id", id), TbPicture.class
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return tbPictures != null && tbPictures.size() > 0 ? tbPictures.get(0) : null;
    }



    @Override
    public List<TbPicture> queryAll() {
        ArrayList<TbPicture> tbPictures = new ArrayList<>();
        try {
            List<Entity> tb_picture = Db.use().findAll("tb_picture");
            for (Entity entity : tb_picture) {
                TbPicture picture = new TbPicture();
                picture.setPictureId((int)entity.get("picture_id"));
                picture.setPictureUrl((String)entity.get("picture_url"));
                tbPictures.add(picture);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return tbPictures;
    }

    @Override
    public boolean update(int id, TbPicture img) {
        int update = 0;
        try {
            update = Db.use().update(
                    Entity.create("tb_picture")
                    .set("picture_url", img.getPictureUrl()),
                    Entity.create("tb_picture")
                            .set("picture_id", id)
                    );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update > 0;
    }

    public static void main(String[] args) {
        ImageDaoImpl imageDao = new ImageDaoImpl();

        // 插入
        // TbPicture tbPicture = new TbPicture();
        // tbPicture.setPictureUrl("hao123.com");
        // boolean add = imageDao.add(tbPicture);
        // System.out.println(add);

        // 删除
        // boolean b = imageDao.removeById(1);
        // System.out.println(b);

        // 查询
        // TbPicture picture = imageDao.queryById(2);
        // System.out.println(picture);

        // 查询所有
        // List<TbPicture> tbPictures = imageDao.queryAll();
        // for (TbPicture tbPicture : tbPictures) {
        //     System.out.println(tbPicture);
        // }

        // 修改
        // TbPicture picture = new TbPicture();
        // picture.setPictureId(1);
        // picture.setPictureUrl("www.google.com");
        // boolean update = imageDao.update(2, picture);
        // System.out.println(update);
    }
}
